package com.egao.common.module.guidance.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.guidance.entity.Lecture;
import com.egao.common.module.guidance.service.LectureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 就业讲座管理控制层
 * @author pyx
 */
@Api(tags = "就业讲座管理")
@RestController
@RequestMapping("${api}")
public class LectureController extends BaseController {
    @Autowired
    private LectureService lectureService;

    @PreAuthorize("hasAuthority('guidance:lecture:list')")
    @ApiOperation("分页就业讲座")
    @OperLog(value = "就业讲座管理", desc = "分页查询就业讲座信息")
    @GetMapping("/guidance/lecture/page")
    public PageResult<Lecture> page(HttpServletRequest request) {
        PageParam<Lecture> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return new PageResult<>(lectureService.listPage(pageParam,getLoginUserId()),pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('guidance:lecture:insert')")
    @ApiOperation("添加就业讲座")
    @OperLog(value = "就业讲座管理", desc = "添加就业讲座", result = true)
    @PostMapping("/guidance/lecture/insert")
    public JsonResult insert(@RequestBody Lecture lecture) {
        return lectureService.insertLecture(lecture);
    }

    @PreAuthorize("hasAuthority('guidance:lecture:update')")
    @ApiOperation("修改就业讲座")
    @OperLog(value = "就业讲座管理", desc = "修改就业讲座", result = true)
    @PutMapping("/guidance/lecture/update")
    public JsonResult update(@RequestBody Lecture lecture) {
        return lectureService.updateLecture(lecture);
    }

    @PreAuthorize("hasAuthority('guidance:lecture:delete')")
    @ApiOperation("删除就业讲座")
    @OperLog(value = "就业讲座管理", desc = "删除就业讲座", result = true)
    @DeleteMapping("/guidance/lecture/delete")
    public JsonResult delete(Integer id) {
        return lectureService.deleteLecture(id);
    }


    @PreAuthorize("hasAuthority('guidance:lecture:detail')")
    @ApiOperation("查询单个就业讲座信息")
    @OperLog(value = "就业讲座管理", desc = "查询单个就业讲座信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "就业讲座id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/guidance/lecture/get")
    public JsonResult listGetId(Integer id) {
        return JsonResult.ok().setData(lectureService.getId(id));
    }

    @PreAuthorize("hasAuthority('guidance:lecture:update')")
    @ApiOperation("生成讲座二维码")
    @OperLog(value = "就业讲座管理", desc = "生成讲座二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "就业讲座id", required = false, dataType = "Integer", paramType = "query"),
    })
    @PutMapping("/guidance/lecture/qr")
    public JsonResult qr(@RequestBody Lecture lecture){
        return lectureService.qr(lecture);
    }

}
